package com.view.speelveld;

import com.example.project_speelveld.R;
import com.level.speelveld.Level;
import com.model.speelveld.GameModel;
import com.tower.speelveld.AirTower;
import com.tower.speelveld.EarthTower;
import com.tower.speelveld.FireTower;
import com.tower.speelveld.Tower;
import com.tower.speelveld.WaterTower;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

// TODO: Auto-generated Javadoc
/**
 * The Class Tower_Menu.
 */
public class Tower_Menu extends LinearLayout {
	
	/** The t. */
	private Tower t;
	
	/** The route. */
	private Level route;
	
	/** The btn fire. */
	private Button btnFire;
	
	/** The btn water. */
	private Button btnWater;
	
	/** The btn air. */
	private Button btnAir;
	
	/** The btn earth. */
	private Button btnEarth;
	
	/** The btn cancel. */
	private Button btnCancel;
	
	/** The btn upgrade. */
	private Button btnUpgrade;
	
	/** The btn sell. */
	private Button btnSell;
	
	/** The btn cancel1. */
	private Button btnCancel1;
	
	/** The txt tower. */
	private TextView txtTower;
	
	/** The txt cost. */
	private TextView txtCost;
	
	/** The txt damage. */
	private TextView txtDamage;
	
	/** The txt radius. */
	private TextView txtRadius;
	
	/** The txt sell price. */
	private TextView txtSellPrice;
	
	/** The buy layout. */
	private LinearLayout buyLayout;
	
	/** The upgrade layout. */
	private LinearLayout upgradeLayout;
	
	/** The game model. */
	private GameModel gameModel;

	/**
	 * Instantiates a new tower_ menu.
	 *
	 * @param context the context
	 * @param attrs the attrs
	 * @param defStyle the def style
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public Tower_Menu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * Instantiates a new tower_ menu.
	 *
	 * @param context the context
	 * @param attrs the attrs
	 */
	public Tower_Menu(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * Instantiates a new tower_ menu.
	 *
	 * @param context the context
	 */
	public Tower_Menu(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * Inits the.
	 */
	private void init()
	{
		//Inflate tower menu.
		LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tower_menu, this);
		
		//Buttons ophalen.
		btnFire = (Button)findViewById(R.id.btnFire);
		btnWater = (Button)findViewById(R.id.btnWater);
		btnAir = (Button)findViewById(R.id.btnAir);
		btnEarth = (Button)findViewById(R.id.btnEarth);
		btnCancel = (Button)findViewById(R.id.btnCancel);
		btnUpgrade = (Button)findViewById(R.id.btnUpgrade);
		btnSell = (Button)findViewById(R.id.btnSell);
		btnCancel1 = (Button)findViewById(R.id.btnCancel1);
		
		//TextViews ophalen.
		txtTower = (TextView)findViewById(R.id.txtTowerType);
		txtCost = (TextView)findViewById(R.id.txtCost);
		txtDamage = (TextView)findViewById(R.id.txtDamage);
		txtRadius = (TextView)findViewById(R.id.txtRadius);
		txtSellPrice = (TextView)findViewById(R.id.txtSellPrice);
		
		//Layouts ophalen.
		buyLayout = (LinearLayout)findViewById(R.id.buyLayout);
		upgradeLayout =(LinearLayout)findViewById(R.id.upgradeLayout);
		
		//OnTouchListener aanmaken.
		buttonOnTouchListener buttonListener = new buttonOnTouchListener();
		
		//Listener toevoegen aan de buttons.
		btnFire.setOnClickListener(buttonListener);
		btnWater.setOnClickListener(buttonListener);
		btnAir.setOnClickListener(buttonListener);
		btnEarth.setOnClickListener(buttonListener);
		btnCancel.setOnClickListener(buttonListener);
		btnUpgrade.setOnClickListener(buttonListener);
		btnSell.setOnClickListener(buttonListener);
		btnCancel1.setOnClickListener(buttonListener);
	}
	
	/**
	 * Sets the upgrade menu visible.
	 *
	 * @param t the new upgrade menu visible
	 */
	public void setUpgradeMenuVisible(Tower t)
	{
		buyLayout.setVisibility(GONE);
		upgradeLayout.setVisibility(VISIBLE);
		txtSellPrice.setVisibility(VISIBLE);
		
		this.t = t;
		
		txtTower.setText(getResources().getString(R.string.txtTowerType) + " " + t.getClass().getSimpleName());
		txtCost.setText(getResources().getString(R.string.txtCost) + " " + t.getupgradeCost());
		txtDamage.setText(getResources().getString(R.string.txtDamage) + " " + t.getDamage());
		txtRadius.setText(getResources().getString(R.string.txtRadius) + " " + t.getRadius());
		txtSellPrice.setText(getResources().getString(R.string.txtSellPrice) + " " + (int)(t.getValue() * 0.75));
	}
	
	/**
	 * Sets the game model.
	 *
	 * @param gameModel the new game model
	 */
	public void setGameModel(GameModel gameModel)
	{
		this.gameModel = gameModel;
	}
	
	/**
	 * Sets the level.
	 *
	 * @param route the new level
	 */
	public void setLevel(Level route)
	{
		this.route = route;
	}
	/*
	 * Inner class voor de buttonOnTouchListeners.
	 */
	/**
	 * The listener interface for receiving buttonOnTouch events.
	 * The class that is interested in processing a buttonOnTouch
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addbuttonOnTouchListener<code> method. When
	 * the buttonOnTouch event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see buttonOnTouchEvent
	 */
	private class buttonOnTouchListener implements OnClickListener
	{
		
		/* (non-Javadoc)
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//Als view een btnFire is.
			if(v.equals(btnFire))
			{
				//Nieuwe fireTower aanmaken en de textViews zetten.
				FireTower ft = new FireTower(0, 0, 1, 100, 90, null);
				setMenu(ft);
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.FIRETOWER;
				
				return ;
			}
			//Als view een btnWater is.
			else if(v.equals(btnWater))
			{
				//Nieuwe waterTower aanmaken en de textViews zetten.
				WaterTower wt = new WaterTower(0, 0, 1, 75, 68, null);
				setMenu(wt);
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.WATERTOWER;
				
				return ;
			}
			//Als view een btnAir is.
			else if(v.equals(btnAir))
			{
				//Nieuwe airTower aanmaken en de textViews zetten.
				AirTower at = new AirTower(0, 0, 2, 50, 45, null);
				setMenu(at);
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.AIRTOWER;
				
				return ;
			}
			//Als view een btnEarth is.
			else if(v.equals(btnEarth))
			{
				//Nieuwe earthTower aanmaken en de textViews zetten.
				EarthTower et = new EarthTower(0, 0, 2, 125, 90, null);
				setMenu(et);
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.EARTHTOWER;
				
				return ;
			}
			//Als view een btnCancel is.
			else if(v.equals(btnCancel))
			{
				//TextViews leegmaken.
				txtTower.setText(getResources().getString(R.string.txtTowerType));
				txtCost.setText(getResources().getString(R.string.txtCost));
				txtDamage.setText(getResources().getString(R.string.txtDamage));
				txtRadius.setText(getResources().getString(R.string.txtRadius));
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.CELL;
				
				return ;
			}
			//Als view een btnUpgrade is.
			else if(v.equals(btnUpgrade))
			{
				if(t.haveUpgrade() == true)
				{
					if(t.getupgradeCost() <= gameModel.getMoney())
					{
						gameModel.removeMoney(t.getupgradeCost());
						int tempRadius = t.getRadius();
						t.upgrade();
						txtDamage.setText(getResources().getString(R.string.txtDamage) + " " + t.getDamage());
						txtCost.setText(getResources().getString(R.string.txtCost) + " " + t.getupgradeCost());
						txtRadius.setText(getResources().getString(R.string.txtRadius) + " " + t.getRadius());
						if(tempRadius != t.getRadius())
						{
							t.setRangePath(route.getInRangePath(t));
						}						
						return ;
					}
				}
				
			}
			//Als view een btnSell is.
			else if(v.equals(btnSell))
			{
				if(t != null)
				{

					//TextViews leegmaken.
					txtTower.setText(getResources().getString(R.string.txtTowerType));
					txtCost.setText(getResources().getString(R.string.txtCost));
					txtDamage.setText(getResources().getString(R.string.txtDamage));
					txtRadius.setText(getResources().getString(R.string.txtRadius));
					
					//ToAdd de juiste waarde geven.
					GameView.toAdd = GameView.CELL;
					
					gameModel.addMoney(t.sell());
					
					buyLayout.setVisibility(VISIBLE);
					upgradeLayout.setVisibility(GONE);
					txtSellPrice.setVisibility(GONE);
					
					t = null;
				}
				
				return ;
			}
			//Als view een btnCancel1 is.
			else if(v.equals(btnCancel1))
			{
				//TextViews leegmaken.
				txtTower.setText(getResources().getString(R.string.txtTowerType));
				txtCost.setText(getResources().getString(R.string.txtCost));
				txtDamage.setText(getResources().getString(R.string.txtDamage));
				txtRadius.setText(getResources().getString(R.string.txtRadius));
				
				//ToAdd de juiste waarde geven.
				GameView.toAdd = GameView.CELL;
				
				buyLayout.setVisibility(VISIBLE);
				upgradeLayout.setVisibility(GONE);
				txtSellPrice.setVisibility(GONE);
				
				t = null;
				
				return ;
			}
			return ;
		}
	}
		
		/*
		 * De text van de TextViews zetten met de juiste gegevens,
		 * aan de hand van de tower die is meegegeven.
		 */
		/**
		 * Sets the menu.
		 *
		 * @param t the new menu
		 */
		private void setMenu(Tower t)
		{
			txtTower.setText(getResources().getString(R.string.txtTowerType) + " " + t.getClass().getSimpleName());
			txtCost.setText(getResources().getString(R.string.txtCost) + " " + t.getValue());
			txtDamage.setText(getResources().getString(R.string.txtDamage) + " " + t.getDamage());
			txtRadius.setText(getResources().getString(R.string.txtRadius) + " " + t.getRadius());
		}

		
		
	
}
